import 'package:demo/config/app_resource.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:demo/component/component_image.dart';
import 'package:demo/component/component_video.dart';

double maxHeight = 400;
double minHeight = 50;
double maxWidth = 180;

// 聊天=消息=视频
class ChatMessageVideo extends StatelessWidget {
  final Map<String, dynamic> content;
  final double? size;
  const ChatMessageVideo(
    this.content, {
    super.key,
    this.size,
  });

  @override
  Widget build(BuildContext context) {
    String data = content['data'];
    if (size != null) {
      return GestureDetector(
        onTap: () {
          Get.to(
            ComponentVideo(
              path: data,
            ),
            transition: Transition.topLevel,
          );
        },
        child: Stack(
          alignment: Alignment.center,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(4),
              child: ComponentImage(
                AppImage.error,
                ImageType.file,
                fit: BoxFit.cover,
                width: size!,
                height: size!,
              ),
            ),
            const Positioned(
              child: Icon(
                Icons.play_circle_outline,
                size: 20,
                color: Colors.white,
              ),
            ),
          ],
        ),
      );
    }
    return GestureDetector(
      onTap: () {
        Get.to(
          ComponentVideo(
            path: data,
          ),
          transition: Transition.topLevel,
        );
      },
      child: Stack(
        alignment: Alignment.center,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Container(
              constraints: BoxConstraints(
                maxHeight: maxHeight,
                minHeight: minHeight,
              ),
              width: maxWidth,
              child: ComponentImage(
                AppImage.error,
                ImageType.asset,
                fit: BoxFit.contain,
                width: double.infinity,
                height: _calculate(content),
              ),
            ),
          ),
          const Positioned(
            child: Icon(
              Icons.play_circle_outline,
              size: 50,
              color: Colors.white,
            ),
          ),
        ],
      ),
    );
  }

  // 计算
  _calculate(Map<String, dynamic> content) {
    int width = content['width'] ?? 200;
    int height = content['height'] ?? 200;
    double value = height / width * maxWidth;
    if (value > maxHeight) {
      return maxHeight;
    }
    if (value < minHeight) {
      return minHeight;
    }
    return value;
  }
}
